﻿package mortal.game.view.copy.treasureHunt.view
{
    import Message.Public.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.treasureHunt.*;
    import mortal.game.view.copy.treasureHunt.renderer.*;
    import mortal.game.view.mainUI.smallMap.view.*;
    import mortal.game.view.vip.*;
    import mortal.mvc.core.*;

    public class TreasureHuntDetailWin extends CopyInfoPanelBase
    {
        private var _boxItemList:GTileList;
        private var _smallMap:SmallMapEmbedView;
        private var _leftTime:SecTimerBitmapView;
        private var _silverBox:BaseItem;
        private var _takeRewardBtn:GButton;
        private var _vipInfo:GTextFiled;
        private var _canTakeNum:GTextFiled;
        private static var _instance:TreasureHuntDetailWin;

        public function TreasureHuntDetailWin()
        {
            super(ImagesConst.copyInfoPanelTitle_treasureHunt);
            this.layer = LayerManager.uiLayer;
            this.mouseEnabled = false;
            if (_instance)
            {
                throw new Error("TreasureHuntDetailWin 单例");
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            _bodySprite.x = _bodySprite.x - 5;
            _bodySprite.y = _bodySprite.y - 6;
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.TreasureHuntDetailBg, 0, 0, _bodySprite));
            this._leftTime = UIFactory.secTimerBitmapView(105, 274 + 11 + 5, "DoubleRewardTimerNum.png", 18.18, 27, 0, _bodySprite, 10);
            this._leftTime.customCharIndex = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, ::10};
            this._leftTime.setParse("mm:ss");
            this._smallMap = UIFactory.smallMapEmbedView(ImagesConst.TreasureHuntMiniMap, 12 + 6, 64 - 16, _bodySprite, true);
            this._smallMap.run();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.TreasureHuntCurrGetBox, 85 - 2 - 61 + 5, 291 - 3 - 40 + 2, _bodySprite));
            this._boxItemList = UIFactory.tileList(16 - 3 + 70 + 30, 314 - 65 - 13, 148, 45, _bodySprite);
            this._boxItemList.columnWidth = 45;
            this._boxItemList.rowHeight = 42;
            this._boxItemList.horizontalGap = 0;
            this._boxItemList.verticalGap = 0;
            this._boxItemList.setStyle("skin", new Bitmap());
            this._boxItemList.setStyle("cellRenderer", TreasureHuntBoxListCellRenderer);
            this._vipInfo = UIFactory.gTextField("", 7 + 5, 334 + 17, 75, 39, _bodySprite);
            this._vipInfo.multiline = true;
            this._vipInfo.wordWrap = true;
            this._silverBox = UICompomentPool.getUICompoment(BaseItem);
            this._silverBox.itemCode = GameConst.TreasureBaiyinBoxCode;
            this._silverBox.x = 80 - 5 + 7;
            this._silverBox.y = 340 + 3 + 8;
            _bodySprite.addChild(this._silverBox);
            this.pushUIToDisposeVec(this._silverBox);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40941), 122 + 2, 343 + 17, 64, 25, _bodySprite, GlobalStyle.textFormatlan));
            this._canTakeNum = UIFactory.gTextField("", 179 - 2, 343 + 17, 25, 25, _bodySprite);
            this._takeRewardBtn = UIFactory.gButton(Language.getString(40942), 197 + 2, 348 + 12, 50, 22, _bodySprite, "RedButton");
            this._takeRewardBtn.configEventListener(MouseEvent.CLICK, this.onTakeRewardHandler);
            this.initItemList();
            this.updateLeftTime();
            this.updateTakeState();
            this.updateVipInfo();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.TreasureHuntBoxInfoUpdate, this.onTreasureHuntBoxInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.onVipLevelUpHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.TreasureHuntBoxInfoUpdate, this.onTreasureHuntBoxInfoUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.onVipLevelUpHandler);
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            x = Global.stage.stageWidth - 254;
            y = 172 - 25;
            return;
        }// end function

        private function initItemList() : void
        {
            var _loc_1:Array = [];
            var _loc_2:* = new TreasureHuntBoxInfo();
            var _loc_3:* = new ItemData(GameConst.TreasureBaiyinBoxCode);
            var _loc_4:* = GameConst.TreasureBaiyinBossCode;
            _loc_2.itemData = _loc_3;
            _loc_2.bossCode = _loc_4;
            _loc_1.push(_loc_2);
            var _loc_5:* = new TreasureHuntBoxInfo();
            var _loc_6:* = new ItemData(GameConst.TreasureHuangjinBoxCode);
            var _loc_7:* = GameConst.TreasureHuangjinBossCode;
            _loc_5.itemData = _loc_6;
            _loc_5.bossCode = _loc_7;
            _loc_1.push(_loc_5);
            var _loc_8:* = new TreasureHuntBoxInfo();
            var _loc_9:* = new ItemData(GameConst.TreasureZijinBoxCode);
            var _loc_10:* = GameConst.TreasureZijinBossCode;
            _loc_8.itemData = _loc_9;
            _loc_8.bossCode = _loc_10;
            _loc_1.push(_loc_8);
            var _loc_11:* = new DataProvider();
            _loc_11.addItems(_loc_1);
            this._boxItemList.dataProvider = _loc_11;
            this._boxItemList.drawNow();
            return;
        }// end function

        private function updateLeftTime() : void
        {
            var _loc_1:Date = null;
            var _loc_2:Date = null;
            var _loc_3:Date = null;
            var _loc_4:int = 0;
            if (Cache.instance.copy.treasureHunt.treasureHuntTimeInfo)
            {
                _loc_1 = ClockManager.instance.nowDate;
                _loc_2 = Cache.instance.copy.treasureHunt.treasureHuntTimeInfo.startDt;
                _loc_3 = Cache.instance.copy.treasureHunt.treasureHuntTimeInfo.endDt;
                _loc_4 = (_loc_3.time - _loc_1.time) * 0.001;
                this._leftTime.setLeftTime(_loc_4);
            }
            return;
        }// end function

        private function onTreasureHuntBoxInfoUpdate(param1:Object) : void
        {
            var _loc_3:Dictionary = null;
            var _loc_2:* = param1 as SRaiderPlayerMsg;
            if (_loc_2)
            {
                _loc_3 = _loc_2.treasureDict;
                Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureHuntUpdateBoxNum, _loc_3));
            }
            return;
        }// end function

        private function updateTakeState() : void
        {
            var _loc_1:* = Cache.instance.vip.getVipFreeTimesByType(EVipRecordType._EVipRecordTypeRaiderReward);
            var _loc_2:* = Cache.instance.vip.level;
            var _loc_3:* = VipUtil.getVipMinLevelByType(EVipRecordType._EVipRecordTypeRaiderReward);
            if (_loc_2 < _loc_3)
            {
                this._takeRewardBtn.visible = false;
            }
            else
            {
                this._takeRewardBtn.visible = true;
                if (_loc_1 > 0)
                {
                    this._takeRewardBtn.filters = [FilterConst.colorFilter2];
                    this._takeRewardBtn.mouseEnabled = false;
                    EffectManager.glowFilterUnReg(this._takeRewardBtn);
                }
                else
                {
                    this._takeRewardBtn.filters = [];
                    this._takeRewardBtn.mouseEnabled = true;
                    EffectManager.glowFilterReg(this._takeRewardBtn);
                }
            }
            return;
        }// end function

        private function onTakeRewardHandler(event:MouseEvent) : void
        {
            GameProxy.player.getVipReWard(Cache.instance.vip.level, EVipRecordType._EVipRecordTypeRaiderReward, this.onTakeSuccHandler);
            return;
        }// end function

        private function onTakeSuccHandler() : void
        {
            EffectManager.glowFilterUnReg(this._takeRewardBtn);
            this._takeRewardBtn.filters = [FilterConst.colorFilter2];
            this._takeRewardBtn.mouseEnabled = false;
            var _loc_1:* = this._silverBox.localToGlobal(new Point(0, 0));
            FlyToNavbarTool.flyToBackPack(this._silverBox.bitmapdata, _loc_1);
            return;
        }// end function

        private function updateVipInfo() : void
        {
            var _loc_3:String = null;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_1:* = Cache.instance.vip.level;
            var _loc_2:* = VipUtil.getVipMinLevelByType(EVipRecordType._EVipRecordTypeRaiderReward);
            if (_loc_1 < _loc_2)
            {
                this._vipInfo.text = Language.getStringByParam(40943, _loc_2);
                _loc_3 = VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeRaiderReward, _loc_2);
                _loc_4 = int(_loc_3.split("#")[1]);
                this._canTakeNum.text = "*" + _loc_4;
            }
            else
            {
                this._vipInfo.htmlText = Language.getStringByParam(40944, _loc_1);
                _loc_5 = VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeRaiderReward, _loc_1);
                _loc_6 = int(_loc_5.split("#")[1]);
                this._canTakeNum.text = "*" + _loc_6;
            }
            return;
        }// end function

        private function onVipLevelUpHandler(param1:Object) : void
        {
            this.updateTakeState();
            this.updateVipInfo();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._boxItemList.dispose();
            this._leftTime.dispose();
            this._smallMap.dispose();
            this._takeRewardBtn.dispose();
            this._silverBox.dispose();
            this._vipInfo.dispose();
            this._canTakeNum.dispose();
            this._boxItemList = null;
            this._leftTime = null;
            this._smallMap = null;
            this._takeRewardBtn = null;
            this._silverBox = null;
            this._vipInfo = null;
            this._canTakeNum = null;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : TreasureHuntDetailWin
        {
            if (_instance == null)
            {
                _instance = new TreasureHuntDetailWin;
            }
            return _instance;
        }// end function

    }
}
